//
//  TodayLiveViewController.m
//  penzer
//
//  Created by caiyc on 2018/10/2.
//  Copyright © 2018年 caiyc. All rights reserved.
//

#import "TodayLiveViewController.h"
#import "TodayLive_Cell.h"
#import "LiveViewController.h"
#import "OYCountDownManager.h"
@interface TodayLiveViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableViewq;
@property(nonatomic,strong)NSMutableArray *dataSouce;
@property(nonatomic,strong)NSString *now_time;
@property(assign)NSInteger pageIndex;
@end

@implementation TodayLiveViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(refresh) name:@"acpt" object:nil];
    //self.view.backgroundColor = [UIColor blueColor];
    self.tableViewq.delegate = self;
    self.tableViewq.dataSource = self;
    self.tableViewq.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableViewq.backgroundColor = [UIColor clearColor];
    //self.tableViewq.backgroundColor = [];
    self.tableViewq.layer.cornerRadius = 5;
    self.tableViewq.layer.masksToBounds = 1;
    self.tableViewq.showsVerticalScrollIndicator = 0;
     [kCountDownManager start];
    self.pageIndex = 1;
   self.tableViewq.mj_header  = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refresh)];
    self.tableViewq.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self
                                                                    refreshingAction:@selector(loadLiveLis)];
    [self loadLiveLis];
    // Do any additional setup after loading the view from its nib.
}


-(void)refresh
{
    self.pageIndex = 1;
    [self.dataSouce removeAllObjects];
    [self loadLiveLis];
}

//获取直播列表
-(void)loadLiveLis
{
    if(self.pageIndex==1){
    [self.dataSouce removeAllObjects];
    }
    NSDictionary *param = @{@"ac":@"store",
                            @"op":@"activity",
                            @"ctrl":@"live_anchor",
                            @"ta":@"list",
                            @"status":@"1",
                            @"page":[NSString stringWithFormat:@"%ld",self.pageIndex]
                            };
    [self weqPostHandle:param success:^(id response) {
        [self.tableViewq.mj_header endRefreshing];
         [self.tableViewq.mj_footer endRefreshing];
        
        if(response){
            [kCountDownManager reload];
            [self.dataSouce addObjectsFromArray:response[@"data"][@"list"]];
            NSArray *datas = response[@"data"][@"list"];
            if(datas.count>0){
            self.reloadBage(self.dataSouce.count);
            self.now_time = [NSString stringWithFormat:@"%@",response[@"data"][@"service_time"]];
            [self.tableViewq reloadData];
            self.pageIndex++;
            }
       }
    } failure:^(NSError *error) {
        [self.tableViewq.mj_header endRefreshing];
         [self.tableViewq.mj_footer endRefreshing];
    }];
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSouce.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 220;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellId = @"cell";
    TodayLive_Cell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if(!cell){
        cell = [[[NSBundle mainBundle]loadNibNamed:@"TodayLive_Cell" owner:self options:nil]lastObject];
    }
    cell.enter=^(){
        LiveViewController *vc = [[LiveViewController alloc]init];
        vc.live_id = [NSString stringWithFormat:@"%@",self.dataSouce[indexPath.row][@"activityid"]];
        //bdlive
        NSDictionary *dic = [self.dataSouce objectAtIndex:indexPath.row];
        vc.live_Url = self.dataSouce[indexPath.row][@"bdlive"];
        vc.roomNum = [NSString stringWithFormat:@"%@",self.dataSouce[indexPath.row][@"room_number"]];
        [self pushSecondVC:vc];
    };
    cell.call=^(){
        NSString *telStr = [NSString stringWithFormat:@"tel:%@",self.dataSouce[indexPath.row][@"telephone"]];
        NSURL *url = [NSURL URLWithString:telStr];
        [[UIApplication sharedApplication]openURL:url];
    };
    
    [cell bindData:self.dataSouce[indexPath.row]andNowTime:self.now_time];
    return cell;
}



#pragma mark---setter&&getter
-(NSMutableArray *)dataSouce{
    if(!_dataSouce){
        _dataSouce = [NSMutableArray array];
    }
    return _dataSouce;
}
@end
